const dayjs = require("dayjs");
function sendFun(res, myPersonResult, zhi) {
  if (myPersonResult && myPersonResult.affectedRows > 0) {
    res.send({
      code: 200,
      msg: "success",
      data: zhi + myPersonResult.affectedRows + "条成功",
    });
  } else {
    res.send({
      code: 0,
      msg: "fail",
      data: zhi + "失败",
    });
  }
}
exports.getLists = async function (req, res) {
  const {userId} = req.user;
  const sql = `SELECT *  FROM node_rob WHERE user_id = ${userId}`;
  const sql_0 = `SELECT * , DATE_FORMAT(end_time, '%m.%d') AS end_time,  DATE_FORMAT(start_time, '%m.%d') AS start_time FROM node_ip order by sort`;
  const myrobResult = await global.query(sql);
  const myPersonResult = await global.query(sql_0);
  console.log("myPersonResult", myPersonResult)
  let isLike = [];
  if (myrobResult.length > 0) {
    myrobResult.map((item) => {
      isLike.push(item["ip_id"]);
    });
  }
  myPersonResult.map((item) => {
    item["is_link"] = isLike != "undefined" && isLike.includes(item["id"]);
  });
  res.send({
    code: 200,
    msg: "success",
    data: { list: myPersonResult },
  });
};
exports.getIp = async function (req, res) {
  const { id } = req.query;
  if(!id || id == undefined || id == "undefined"){
    res.send({
      code: 0,
      msg: "fail",
      data: "请输入ID",
    });
    return false
  }
  const {userId} = req.user;
  const sql = `SELECT *  FROM node_rob WHERE user_id = ${userId} AND ip_id = ${id}`;
  const sql_0 = `SELECT * , DATE_FORMAT(end_time, '%m.%d') AS end_time,  DATE_FORMAT(start_time, '%m.%d') AS start_time, DATE_FORMAT(stop_time, '%m.%d') AS stop_time FROM node_ip WHERE id=?`;
  const myrobResult = await global.query(sql);
  const myPersonResult = await global.query(sql_0, [id]);
  if (myrobResult.length > 0) {
    myPersonResult[0].is_link = true
  }
  res.send({
    code: 200,
    msg: "success",
    data: myPersonResult[0],
  });
};
exports.updataIp = async function (req, res) {
  const create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
  const { id } = req.body;
  const column = []
  const columnapp = []
  const columnappArr = []
  const value = []
  Object.keys(req.body).map((item) => {
    if (req.body[item] && item != 'id') {
      column.push(item + '=?')
      columnapp.push(item)
      columnappArr.push('?')
      value.push(req.body[item])
    }
  })
  value.push(create_time)
  if (id) {
    value.push(id)
    const sql_0 = `UPDATE node_ip SET ${column.join(",")},create_time=? WHERE id = ?`;
    const myPersonResult = await global.query(sql_0, value);
    sendFun(res, myPersonResult, "更新")
  } else {
    // title,top_img,price,inventory,start_time,end_time,create_time
    const sql_0 = `INSERT INTO node_ip(${columnapp.join(',')},create_time) VALUES(${columnappArr.join(",")},?)`;
    const myPersonResult = await global.query(sql_0, value);
    sendFun(res, myPersonResult, "添加")
  }

};
exports.delIp = async function (req, res) {
  const { id } = req.query;
  const sql_0 = `DELETE FROM node_ip WHERE id=?`;
  const myPersonResult = await global.query(sql_0, [id]);
  sendFun(res, myPersonResult, "删除")
};
exports.getRob = async function (req, res) {
  const sql_0 = `SELECT * FROM node_rob`;
  const myResult = await global.query(sql_0);
  res.send({
    code: 200,
    msg: "success",
    data: { list: myResult },
  });
};
exports.setRob = async function (req, res) {
  const {userId} = req.user;
  const { ip_id } = req.body;
  if(!ip_id){
    res.send({
      code: 0,
      msg: "fail",
      data: "请输入ID",
    });
    return false
  }
  let people = 1
  // 获取IP信息
  const sql_3 = `SELECT * FROM node_ip WHERE id=?`;
  const getData1 = await global.query(sql_3, [ip_id]);
  if (getData1.length > 0) {
    if(getData1[0].people) {
      people = getData1[0].people + 1
    }
  } else {
    res.send({
      code: 0,
      msg: "fail",
      data: "商品已下架请选择其他商品",
    });
    return false
  }
  // 添加人数
  const sql_4 = `UPDATE node_ip SET people=? WHERE id = ?`;
  const myResult = await global.query(sql_4, [people, ip_id]);
  if (myResult && myResult.affectedRows == 0) {
    res.send({
      code: 0,
      msg: "fail",
      data: "抢购失败",
    });
    return false
  }

  // 查看是否点过赞
  const my_time = dayjs().format("YYYY-MM-DD");
  const sql_0 = `SELECT * FROM node_rob WHERE user_id=? AND create_time = ?`;
  const getData = await global.query(sql_0, [userId, my_time]);

  if (getData.length > 0) {
    res.send({
      code: 0,
      msg: "fail",
      data: "您已经抢过了,明天再来",
    });
    return false
  }else{
    const sql_2 = `INSERT INTO node_rob(user_id,ip_id,create_time) VALUES(?,?,?)`;
    const myResult = await global.query(sql_2, [userId, ip_id, my_time]);
    sendFun(res, myResult, "添加")
  }
};